package com.triplelink.util;

import java.io.StringReader;
import java.io.StringWriter;
import java.text.Normalizer;
import java.text.Normalizer.Form;
import java.util.Locale;
import java.util.regex.Pattern;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public final class StringUtil {
	
	private StringUtil() {
		
	}
	
	private static String defaultEncode;
	
	public static String getDefaultEncode() {
		
		if (defaultEncode == null) {
			defaultEncode = System.getProperty("file.encoding");
		}
		
		return defaultEncode;
	}
	
	
	private static final Pattern NONLATIN = Pattern.compile("[^\\w-]");
	private static final Pattern WHITESPACE = Pattern.compile("[\\s]");
	
	public static boolean isBlank(String str) {
		
		if (str == null)
			return true;
		
		return str.isEmpty();
	}
	
	public static String toSlug(String input) {
		
		String nowhitespace = WHITESPACE.matcher(input).replaceAll("-");
		String normalized = Normalizer.normalize(nowhitespace, Form.NFD);
		String slug = NONLATIN.matcher(normalized).replaceAll("");
		
		return slug.toLowerCase(Locale.ENGLISH);
	}
	
	public static String prettyFormat(String input, int indent) {
		
	    try {
	    	
	        Source xmlInput = new StreamSource(new StringReader(input));
	        StringWriter stringWriter = new StringWriter();
	        StreamResult xmlOutput = new StreamResult(stringWriter);
	        TransformerFactory transformerFactory = TransformerFactory.newInstance();
	        transformerFactory.setAttribute("indent-number", indent);
	        Transformer transformer = transformerFactory.newTransformer(); 
	        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	        transformer.transform(xmlInput, xmlOutput);
	        return xmlOutput.getWriter().toString();
	        
	    } catch (Exception e) {
	    	
	        throw new RuntimeException(e);
	    }
	}

	public static String prettyFormat(String input) {
	    return prettyFormat(input, 2);
	}
	
	public static boolean isBlankOrNull(String st) {
		
		if (st == null)
			return true;
		
		return st.trim().equals("");
	}
	
	public static String gerantirSeExistir(String prefixo, String texto, String sufixo) {
		
		if (StringUtil.isBlankOrNull(texto)) {
			
			return null;
		}
		
		StringBuilder sb = new StringBuilder();
		
		if (prefixo != null) {
			
			if (prefixo.length() > texto.length() || !texto.substring(0, prefixo.length()).equals(prefixo)) {
				
				sb.append(prefixo);
			}
		}
		
		sb.append(texto);
		
		if (sufixo != null) {
			
			if (sufixo.length() > texto.length() || !texto.substring(texto.length() - sufixo.length(), texto.length()).equals(sufixo)) {
				
				sb.append(sufixo);
			}
		}
		
		return sb.toString();
	}
	
	public static String concatenar(String prefixo, String valor, String sufixo) {
		
		StringBuilder sb = new StringBuilder();
		
		if (prefixo != null) {
			
			sb.append(prefixo);
		}

		sb.append(valor);

		
		if (sufixo != null) {
			
			sb.append(sufixo);
		}
		
		return sb.toString();
	}
	
	public static String garantirPontoFinal(String prefixo, String texto) {
		
		StringBuilder sb = new StringBuilder();
		
		if (!StringUtil.isBlankOrNull(texto)) {
			
			texto = texto.trim();
			
			if (prefixo != null) {
				sb.append(prefixo);
			}
			
			sb.append(texto);
			
			char ultimoCaracter = texto.charAt(texto.length() - 1);
			
			if (ultimoCaracter != '.' && ultimoCaracter != '!') {
				
				sb.append('.');
			}
			
			return sb.toString();
		}
		
		return null;
	}
}